package com.gxp.system.service.impl;

import com.gxp.common.core.text.Convert;
import com.gxp.common.utils.DateUtils;
import com.gxp.common.utils.DictUtils;
import com.gxp.common.utils.StringUtils;
import com.gxp.system.domain.ZkInstrument;
import com.gxp.system.mapper.ZkInstLogMapper;
import com.gxp.system.mapper.ZkInstrumentMapper;
import com.gxp.system.mapper.ZknjExpTemplateMapper;
import com.gxp.system.service.IZkInstrumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 仪器Service业务层处理
 * 
 * @author zh
 * @date 2024-03-08
 */
@Service
public class ZkInstrumentServiceImpl implements IZkInstrumentService 
{
    @Autowired
    private ZkInstrumentMapper zkInstrumentMapper;
    @Autowired
    private ZkInstLogMapper instLogMapper;
    @Autowired
    private ZknjExpTemplateMapper expTemplateMapper;

    /**
     * 查询仪器
     * 
     * @param instId 仪器主键
     * @return 仪器
     */
    @Override
    public ZkInstrument selectZkInstrumentByInstId(Long instId)
    {
        ZkInstrument instrument = zkInstrumentMapper.selectZkInstrumentByInstId(instId);
        instrument.setInstLogs(instLogMapper.selectInstLogsByInstId(instrument.getInstId()));
        return instrument;
    }

    /**
     * 查询仪器列表
     * 
     * @param zkInstrument 仪器
     * @return 仪器
     */
    @Override
    public List<ZkInstrument> selectZkInstrumentList(ZkInstrument zkInstrument)
    {
        return zkInstrumentMapper.selectZkInstrumentList(zkInstrument);
    }

    /**
     * 新增仪器
     * 
     * @param zkInstrument 仪器
     * @return 结果
     */
    @Override
    public int insertZkInstrument(ZkInstrument zkInstrument)
    {
        zkInstrument.setCreateTime(DateUtils.getNowDate());
        zkInstrument.setStatus(0l);
        zkInstrument.setApproveType("Add Instrument");
        zkInstrument.setIsLock(0l);
        return zkInstrumentMapper.insertZkInstrument(zkInstrument);
    }

    /**
     * 修改仪器
     * 
     * @param zkInstrument 仪器
     * @return 结果
     */
    @Override
    public int updateZkInstrument(ZkInstrument zkInstrument)
    {
        zkInstrument.setUpdateTime(DateUtils.getNowDate());
        zkInstrument.setIsLock(zkInstrument.getStatus());
        zkInstrument.setStatus(2l);
        zkInstrument.setApproveType("Update Instrument");
        zkInstrument.setComment("");
        return zkInstrumentMapper.updateZkInstrument(zkInstrument);
    }

    /**
     * 批量删除仪器
     * 
     * @param instIds 需要删除的仪器主键
     * @return 结果
     */
    @Override
    public int deleteZkInstrumentByInstIds(String instIds)
    {
        String[] strArray = Convert.toStrArray(instIds);
        for (String s : strArray) {
            Long instId = Long.valueOf(s);
            ZkInstrument instrument = zkInstrumentMapper.selectZkInstrumentByInstId(instId);
            instrument.setStatus(2l);
            instrument.setApproveType("Delete Instrument");
            instrument.setComment("");
            zkInstrumentMapper.updateZkInstrument(instrument);
        }
        return 1;
    }

    /**
     * 删除仪器信息
     * 
     * @param instId 仪器主键
     * @return 结果
     */
    @Override
    public int deleteZkInstrumentByInstId(Long instId)
    {
        return zkInstrumentMapper.deleteZkInstrumentByInstId(instId);
    }

    /**
     * 修改仪器状态
     * @param instrument
     * @return
     */
    @Override
    public int changeStatus(ZkInstrument instrument) {
        return zkInstrumentMapper.updateZkInstrument(instrument);
    }

    /**
     * 仪器重名校验
     */
    @Override
    public boolean checkInstNameUnique(ZkInstrument instrument) {
        Long instId = StringUtils.isNull(instrument.getInstId()) ? -1L : instrument.getInstId();
        ZkInstrument info = zkInstrumentMapper.selectZkInstrumentByName(instrument.getInstName());
        if (StringUtils.isNotNull(info) && info.getInstId().longValue() != instId.longValue())
        {
            return false;
        }
        return true;
    }

    /**
     * 根据项目申请查询可用仪器列表
     * @param reqId
     * @return
     */
    @Override
    public List<ZkInstrument> selectZkInstrumentListByReqId(Long reqId) {
        List<ZkInstrument> instruments = zkInstrumentMapper.selectZkInstrumentListByReqId(reqId);
        List<ZkInstrument> collect = instruments.stream().filter(instrument -> instrument.getStatus() == 0).collect(Collectors.toList());
        return collect;
    }

    /**
     * 仪器审核
     * @param instrument
     * @return
     */
    @Override
    public int approve(ZkInstrument instrument) {
        if(Objects.equals(instrument.getStatus(), Long.valueOf(DictUtils.getDictValue("sys_appr_status", "Approved")))){
            //审核通过
            ZkInstrument instrumentOld = zkInstrumentMapper.selectZkInstrumentByInstId(instrument.getInstId());
            instrument.setStatus(instrumentOld.getIsLock());
            //判断 Add/Update/Delete
            if (instrumentOld.getApproveType().equals("Delete Instrument")){
                //删除模板
                expTemplateMapper.deleteZkExpTemplateByInstId(instrument.getInstId());
                return  zkInstrumentMapper.deleteZkInstrumentByInstId(instrument.getInstId());
            }
        }
        return zkInstrumentMapper.updateZkInstrument(instrument);
    }

    /**
     * hostId重复检验
     */
    @Override
    public boolean checkHostIdUnique(ZkInstrument instrument) {
        Long instId = StringUtils.isNull(instrument.getInstId()) ? -1L : instrument.getInstId();
        ZkInstrument inst = zkInstrumentMapper.selectZkInstrumentByHostId(instrument.getHostId());
        if(StringUtils.isNotNull(inst) && inst.getInstId().longValue() != instId.longValue()) {
            //检验失败，有重名
            return false;
        }
        return true;
    }

    /**
     * 根据hostId查询仪器信息
     * @param hostId
     * @return
     */
    @Override
    public ZkInstrument selectZkInstrumentByHostId(String hostId) {
        return zkInstrumentMapper.selectZkInstrumentByHostId(hostId);
    }
}
